
const db = require('../db/db')
const {paging} = require('../utils/paging')
module.exports = {
    // 添加评论
    addComment: async (ctx,next)=>{
        const {blog_id, content, nickname} = ctx.request.body
        const create_time = new Date()
        await db.promise().query('insert into comment(blog_id,nickname,content,create_time) values(?,?,?,?)',[blog_id, nickname, content,create_time]).then(res=>{
            if(res[0].affectedRows == 1){
                ctx.body = {
                    code:200,
                    msg:"添加成功"
                }
            }
        }).catch(err=>{
            ctx.body = {
                code:201,
                msg:"添加失败"+err
            }
        })
    },
    // 删除评论（恢复评论）
    delComment: async (ctx,next)=>{
        const {comment_id,isdel} = ctx.request.body
        const isdelVal = isdel==0 ? 1:0
        await db.promise().query('update comment set isdel=? where id=?',[isdelVal,comment_id]).then(res=>{
            if(res[0].affectedRows == 1){
                if(isdelVal == 1){
                    ctx.body = {
                        code:200,
                        msg:'删除成功'
                    }
                }else{
                    ctx.body = {
                        code:200,
                        msg:'恢复成功'
                    }
                }
            }
        }).catch(err=>{
            ctx.body = {
                code:201,
                msg:'删除失败'+err
            }
        })
    },
    // 获取单篇评论和全部
    getComment: async (ctx,next)=>{
        if(ctx.request.body.blog_id){
            const {blog_id} = ctx.request.body
            const pageObj = await paging(ctx)
            const startPage = pageObj.startPage
            const endPage = pageObj.endPage
            await db.promise().query('select id from comment where isdel=0').then(async res=>{
                const total = res[0].length
                await db.promise().query('select id,nickname,blog_id,content,create_time,good from comment  where blog_id=? and isdel=0 order by create_time desc limit ?, ? ',[blog_id,startPage,endPage]).then(res=>{
                    ctx.body = {
                        code:1000,
                        msg:"获取成功",
                        comments:res[0],
                        total:total
                    }
                })
            })
        }else{
            const pageObj = await paging(ctx)
            const startPage = pageObj.startPage
            const endPage = pageObj.endPage
            await db.promise().query('select id from comment where isdel=0').then(async res=>{
                const total = res[0].length
                await db.promise().query('select id,nickname,blog_id,content,create_time,good from comment  where  isdel=0 order by create_time desc limit ?, ? ',[startPage,endPage]).then(res=>{
                    ctx.body = {
                        code:1000,
                        msg:"获取成功",
                        comments:res[0],
                        total:total
                    }
                })
            })
        }
    },
    goGood:async(ctx,next)=>{
        const {comment_id} = ctx.request.body
        const [res] = await db.promise().query('select good from comment where id=?',comment_id)
        let num = res[0].good
        let goodNum = num + 1
        await db.promise().query('update comment set good=? where id=?',[goodNum,comment_id]).then(res=>{
           if(res.length > 0){
            ctx.body = {
                code:200,
                msg:'点赞成功'
            }
           }
        })
    }
}
